package com.idanchuang.component.sentinel.runner;

import com.alibaba.csp.sentinel.Constants;
import com.idanchuang.spi.config.IConfig;
import com.zoi7.component.core.base.BaseClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;

/**
 *
 * 监听流控开关
 *
 * @author yjy
 * @date 2020/1/9 9:34
 **/
public class SwitchListenerRunner extends BaseClass implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(SwitchListenerRunner.class);

    @Autowired
    private IConfig iConfig;

    private final String switchKey = "idanchuang.component.sentinel.switchOn";

    @Override
    public void run(String... args) throws Exception {
        iConfig.addChangeListener(() -> {
            boolean onOff = iConfig.getBoolean(switchKey, true);
            if (onOff != Constants.ON) {
                Constants.ON = onOff;
                log.info("Sentinel switch updated, current: {}", onOff);
            }
        });
    }
}
